﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ASoft.Web.Base;

namespace Asoft.Web.Controllers
{
    public class CustomAUserController : EntityController<CustomAUserRepository, CustomAUser>
    {
        [HttpPost]
        public ActionResult Delete()
        {
            var id = int.Parse(Request["CustomAUserId"]);
            Repository.Delete(id);
            return GetJsonResult("success", "删除成功");
        }

        [HttpPost]
        public ActionResult Add()
        {
            var entity = new CustomAUser();
            return ProcessEntity(entity, Repository.Insert, "添加成功");
        }

        public ActionResult ProcessEntity(CustomAUser entity, Action<CustomAUser> action, string msg)
        {
            var status = "error";

            entity.AUserId = int.Parse(Request["AUserId"]);
            entity.CustomId = int.Parse(Request["CustomId"]);

            action(entity);

            status = "success";
            return GetJsonResult(status, msg);

        }

        protected override ASoft.Base.PageResult<CustomAUser> GetIndex1PageResult()
        {
            var result = Repository.GetPageResult(null, PageIndex, PageSize, new string[] { "auserid" }, new string[] { "asc" });
            return result;
        }

        protected override void BeforeIndex1ReturnView()
        {
            var condition = new Dictionary<string, string>();
            condition["Enable"] = "1";
            ViewData["users"] = (new AUserRepository()).GetEntities(condition);
            ViewData["customs"] = (new CustomRepository()).GetEntities(condition);

        }

        protected override void ProcessJQGridEdit(CustomAUser entity)
        {
            entity.CustomId = int.Parse(Request["CustomId"]);
            entity.AUserId = int.Parse(Request["AUserId"]);
        }

        public ActionResult CustomOptions()
        {
            CustomRepository customRepository = new CustomRepository();
            var entities = customRepository.GetEntities(new Dictionary<string, string>() { { "Enable", "1" } });
            var result = string.Join(",", entities.Select(p => string.Format("{0}:\"{1}\"", p.CustomId, p.Code + "." + p.CName)).ToArray());
            return Content("{" + result + "}");
        }

        public ActionResult AUserOptions()
        {
            AUserRepository repository = new AUserRepository();
            var entities = repository.GetEntities(new Dictionary<string, string>() { { "Enable", "1" } });
            var result = string.Join(",", entities.Select(p => string.Format("{0}:\"{1}\"", p.AUserId, p.Code + "." + p.CName)).ToArray());
            return Content("{" + result + "}");
        }
    }
}
